//
//  YLAdressManagerViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/8.
//  Copyright © 2017年 she1323. All rights reserved.
//  地址管理

#import "YLAdressManagerViewController.h"
#import "YLAdressTableViewCell.h"
#import <Masonry.h>
#import "YLAdressSettingViewController.h"
#import "YLAdressManagerViewModel.h"
#import "YLEditAdressViewController.h"
@interface YLAdressManagerViewController ()<YLEditAddressViewControllerDelegate>
@property (nonatomic, strong) YLAdressManagerViewModel *viewModel;
@end

@implementation YLAdressManagerViewController


- (YLAdressManagerViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLAdressManagerViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [[self.models rac_signalForSelector:@selector(removeObjectAtIndex:)] subscribeNext:^(RACTuple * _Nullable x) {
//        NSLog(@"数组变了");
//    }];
    

    self.title = @"地址管理";
    __weak typeof(self) weakSelf = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLAdressTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLAdressTableViewCell cellID]];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(weakSelf.view);
    }];
    
    if (!_models){
          [[self.viewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
               weakSelf.models = weakSelf.viewModel.models;
                [weakSelf.tableView reloadData];
            }];
    }

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"addAddress"] style:UIBarButtonItemStylePlain target:self action:@selector(addAddress)];
}


- (void)addAddress
{
    YLEditAdressViewController *editAddressCon = [[YLEditAdressViewController alloc] init];
    editAddressCon.delegate = self;
    [self.navigationController pushViewController:editAddressCon animated:YES];
}

- (void)edidAddressViewController:(YLEditAdressViewController *)controller addAddress:(YLMyAdressModel *)model
{
    if (self.models) {
        [self.models addObject:model];
//        if (self.delegate && [self.delegate respondsToSelector:@selector(addressManagerViewController:reloadAddresses:)]) {
//            [self.delegate addressManagerViewController:self reloadAddresses:self.models];
//        }
        [self.tableView reloadData];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLAdressTableViewCell cellID]];
    cell.model = self.models[indexPath.section];
    cell.hiddenIcon = NO;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self) weakSelf = self;
    YLAdressSettingViewController *setting = [[YLAdressSettingViewController alloc] init];
    
    setting.deleteAddressBlock = ^(NSInteger index) {
        
        //[weakSelf.models removeObjectAtIndex:index];
    [weakSelf.models removeObjectAtIndex:index];
        
        
//        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(addressManagerViewController:reloadAddresses:)]) {
//            [weakSelf.delegate addressManagerViewController:weakSelf reloadAddresses:weakSelf.viewModel.models];
//        }
        
        [weakSelf.tableView reloadData];
    };
    
    setting.adressModel = self.models[indexPath.section];
    
    setting.index = indexPath.section;
    setting.canDelete = YES;
    if (self.models.count < 2) {
        setting.canDelete = NO;
    }
    
    [self.navigationController pushViewController:setting animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
